/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.transitionseverywhere;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.app.Context;
/**
 * This transition tracks changes to the visibility of target views in the
 * start and end scenes and fades views in or out when they become visible
 * or non-visible. Visibility is determined by both the
 * {@link View#setVisibility(int)} state of the view as well as whether it
 * is parented in the current view hierarchy.
 * <p/>
 * <p>The ability of this transition to fade out a particular view, and the
 * way that that fading operation takes place, is based on
 * the situation of the view in the view hierarchy. For example, if a view was
 * simply removed from its parent, then the view will be added into a {@link
 * ohos.agp.components.componentGroupOverlay} while fading. If a visible view is
 * changed to be {@link View#GONE} or {@link View#INVISIBLE}, then the
 * visibility will be changed to {@link View#VISIBLE} for the duration of
 * the animation. However, if a view is in a hierarchy which is also altering
 * its visibility, the situation can be more complicated. In general, if a
 * view that is no longer in the hierarchy in the end scene still has a
 * parent (so its parent hierarchy was removed, but it was not removed from
 * its parent), then it will be left alone to avoid side-effects from
 * improperly removing it from its parent. The only exception to this is if
 * the previous {@link Scene} was
 * {@link Scene#getSceneForLayout(ohos.agp.components.ComponentContainer, int, ohos.app.Context)
 * created from a layout resource file}, then it is considered safe to un-parent
 * the starting scene view in order to fade it out.</p>
 * <p/>
 * <p>A Fade transition can be described in a resource file by using the
 * tag <code>fade</code>, along with the standard
 * attributes of {@link com.transitionseverywhere.ResourceTable#Fade} and
 * {@link com.transitionseverywhere.ResourceTable#Transition}.</p>
 */
public class Fade extends Visibility {

    static final String PROPNAME_ALPHA = "fade:alpha";
    /**
     * Fading mode used in {@link #Fade(int)} to make the transition
     * operate on targets that are appearing. Maybe be combined with
     * {@link #OUT} to fade both in and out. Equivalent to
     * {@link Visibility#MODE_IN}.
     */
    public static final int IN = Visibility.MODE_IN;
    /**
     * Fading mode used in {@link #Fade(int)} to make the transition
     * operate on targets that are disappearing. Maybe be combined with
     * {@link #IN} to fade both in and out. Equivalent to
     * {@link Visibility#MODE_OUT}.
     */
    public static final int OUT = Visibility.MODE_OUT;
    /**
     * Constructs a Fade transition that will fade targets in and out.
     */
    public Fade() {
    }
    /**
     * Constructs a Fade transition that will fade targets in
     * and/or out, according to the value of fadingMode.
     *
     * @param fadingMode The behavior of this transition, a combination of
     *                   {@link #IN} and {@link #OUT}.
     */
    public Fade(int fadingMode) {
        setMode(fadingMode);
    }



    public Fade(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        super.captureStartValues(transitionValues);
        if (transitionValues.view != null) {
            transitionValues.values.put(PROPNAME_ALPHA, transitionValues.view.getAlpha());
        }
    }

    /**
     * Utility method to handle creating and running the Animator.
     */
    private Animator createAnimation(final Component view, float startAlpha, float endAlpha, TransitionValues values) {
        final float curAlpha = view.getAlpha();
        startAlpha = curAlpha * startAlpha;
        endAlpha = curAlpha * endAlpha;

        if (values != null && values.values.containsKey(PROPNAME_ALPHA)) {
            float savedAlpha = (Float) values.values.get(PROPNAME_ALPHA);
            if (savedAlpha != curAlpha) {
                startAlpha = savedAlpha;
            }
        }

        view.setAlpha(startAlpha);
        final AnimatorProperty animatorProperty = new AnimatorProperty(view);
        animatorProperty.alpha(endAlpha);
        final FadeAnimatorListener listener = new FadeAnimatorListener(view, curAlpha);
        animatorProperty.setStateChangedListener(listener);
        addListener(new TransitionListenerAdapter() {
            @Override
            public void onTransitionEnd(Transition transition) {
                view.setAlpha(curAlpha);
                transition.removeListener(this);
            }
        });
        return animatorProperty;
    }

    @Override
    public Animator onAppear(ComponentContainer sceneRoot, Component view,
                             TransitionValues startValues,
                             TransitionValues endValues) {
        return createAnimation(view, 0, 1, startValues);
    }

    @Override
    public Animator onDisappear(ComponentContainer sceneRoot, final Component view, TransitionValues startValues,
                                TransitionValues endValues) {
        return createAnimation(view, 1, 0, startValues);
    }

    private static class FadeAnimatorListener implements Animator.StateChangedListener {
        private final Component mView;
        private float mEndListenerAlpha;
        private boolean mLayerTypeChanged = false;

        public FadeAnimatorListener(Component view, float endListenerAlpha) {
            mView = view;
            mEndListenerAlpha = endListenerAlpha;
        }

        @Override
        public void onStart(Animator animator) {
        }

        @Override
        public void onStop(Animator animator) {

        }

        @Override
        public void onCancel(Animator animator) {

        }

        @Override
        public void onEnd(Animator animator) {
            mView.setAlpha(mEndListenerAlpha);
            if (mLayerTypeChanged) {
            }
        }

        @Override
        public void onPause(Animator animator) {

        }

        @Override
        public void onResume(Animator animator) {

        }
    }
}